#include <QRegExp>
#include "NewMessage.h"
#include "ui_NewMessage.h"


NewMessage::NewMessage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewMessage)
{
    ui->setupUi(this);

    setWindowIcon(QIcon(":/image/trayicon.png"));
    ui->CcBccFrame->hide();

    VisibleListAddFiles();

    CreateActions();

    AccountsComboBox();
    suggest = new EmailSuggest();
    suggest->EmailContact(accountsList);
}

NewMessage::~NewMessage()
{
    delete ui;
    delete suggest;
}


void NewMessage::AccountsComboBox()
{
    accountsList = settings.AccountsList();
    for(int a=0;a<accountsList.size();a++)
    {
        ui->fromComboBox->addItem(accountsList.at(a));
    }
}

void NewMessage::CreateActions()
{
    connect(ui->fontComboBox,SIGNAL(activated(QString)),this,SLOT(TextFamily(QString)));
    connect(ui->fontSizeBox,SIGNAL(valueChanged(QString)),this,SLOT(TextSize(QString)));
    connect(ui->textAlignButtons,SIGNAL(buttonClicked(QAbstractButton*)),this,SLOT(TextAlign(QAbstractButton*)));

    connect(ui->listAddFiles,SIGNAL(itemChanged(QListWidgetItem*)),this,SLOT(DeleteFile(QListWidgetItem*)));

    connect(ui->toLineEdit,SIGNAL(textEdited(QString)),this,SLOT(AutoSuggest()));
    connect(ui->ccLineEdit,SIGNAL(textEdited(QString)),this,SLOT(AutoSuggest()));
    connect(ui->bccLineEdit,SIGNAL(textEdited(QString)),this,SLOT(AutoSuggest()));
}
void NewMessage::SetFormat(QTextCharFormat &format)
{
    QTextCursor cursor = ui->messageText->textCursor();
    if (!cursor.hasSelection())
        cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);
    ui->messageText->mergeCurrentCharFormat(format);
}

void NewMessage::TextFamily(const QString &family)
{
    QTextCharFormat fmt;
    fmt.setFontFamily(family);
    SetFormat(fmt);
}
void NewMessage::TextSize(const QString &p)
{
    qreal pointSize = p.toFloat();
    if (p.toFloat() > 0)
    {
        QTextCharFormat fmt;
        fmt.setFontPointSize(pointSize);
        SetFormat(fmt);
    }
}

void NewMessage::TextAlign(QAbstractButton *a)
{
    if(a == ui->leftAlignButton)
        ui->messageText->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
    else if (a == ui->centerAlignButton)
        ui->messageText->setAlignment(Qt::AlignHCenter);
    else if (a == ui->rightAlignButton)
        ui->messageText->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
    else if (a == ui->justifyAlignButton)
        ui->messageText->setAlignment(Qt::AlignJustify);

}

void NewMessage::on_ToLabelButton_clicked()
{
    if(ui->CcBccFrame->isVisible())
        ui->CcBccFrame->hide();
    else
        ui->CcBccFrame->show();
}

void NewMessage::on_boldTextButton_clicked()
{

    QTextCharFormat fmt;
    fmt.setFontWeight(ui->boldTextButton->isChecked() ? QFont::Bold : QFont::Normal);
    SetFormat(fmt);
}

void NewMessage::on_italicTextButton_clicked()
{
    QTextCharFormat fmt;
    fmt.setFontItalic(ui->italicTextButton->isChecked());
    SetFormat(fmt);
}

void NewMessage::on_underlineTextButton_clicked()
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(ui->underlineTextButton->isChecked());
    SetFormat(fmt);
}

void NewMessage::on_textColorButton_clicked()
{
    QColor col = QColorDialog::getColor(ui->messageText->textColor(), this);
    if (!col.isValid())
        return;
    QTextCharFormat fmt;
    fmt.setForeground(col);
    SetFormat(fmt);

    QString style = "QPushButton {color:";
    style.append(col.name());
    style.append("}");
    ui->textColorButton->setStyleSheet(style);
}

void NewMessage::on_backgroundTextColor_clicked()
{
    QColor col = QColorDialog::getColor(ui->messageText->textBackgroundColor(), this);
    if (!col.isValid())
        return;
    QTextCharFormat fmt;
    fmt.setBackground(col);
    fmt.setProperty(QTextFormat::FullWidthSelection,true);
    SetFormat(fmt);

    QString style = "QPushButton {color:";
    style.append(col.name());
    style.append("}");
    ui->backgroundTextColor->setStyleSheet(style);
}

void NewMessage::on_addFileButton_clicked()
{
    QStringList fileNames = QFileDialog::getOpenFileNames(this,tr("Open"),QString());
    if(fileNames.isEmpty())
        return;
    AddFiles(fileNames);
}
void NewMessage::VisibleListAddFiles()
{
    if(0 == ui->listAddFiles->count())
        ui->listAddFiles->hide();
    else
        ui->listAddFiles->show();
}

void NewMessage::AddFiles(QStringList fn)
{
    for(int a=0;a<fn.size();a++)
    {

        if(!addFilesPath.contains(fn.at(a)))
        {
            QFileInfo fileInfo(fn.at(a));
            QListWidgetItem *fileCheckBox = new QListWidgetItem(fileInfo.fileName());
            QFileIconProvider ip;
            QIcon icon = ip.icon(fileInfo);

            fileCheckBox->setCheckState(Qt::Checked);
            fileCheckBox->setIcon(icon);

            ui->listAddFiles->addItem(fileCheckBox);

            addFilesPath.append(fn.at(a));
        }

    }
    VisibleListAddFiles();
}

void NewMessage::DeleteFile(QListWidgetItem *fn)
{
    int i = addFilesPath.indexOf(QRegExp(fn->text().prepend(".*")));
    addFilesPath.removeAt(i);
    delete fn;
    VisibleListAddFiles();
}

void NewMessage::on_messageText_currentCharFormatChanged(const QTextCharFormat &format)
{
    //Family
    QString font = format.fontFamily();
    ui->fontComboBox->setCurrentFont(QFont(font));

    //Size
    qreal size = format.fontPointSize();
    if(size != 0)
        ui->fontSizeBox->setValue(size);
    else
        ui->fontSizeBox->setValue(12);

    //Bold
    int weight = format.fontWeight();
    if(50 == weight)
    {
        ui->boldTextButton->setChecked(false);
    }
    else if(75 == weight)
    {
        ui->boldTextButton->setChecked(true);
    }

    //Italic
    ui->italicTextButton->setChecked(format.fontItalic());


    //Underline
    ui->underlineTextButton->setChecked(format.fontUnderline());

    //Color
    QBrush colT = format.foreground();
    QString styleT = "QPushButton {color:";
    styleT.append(colT.color().name());
    styleT.append("}");
    ui->textColorButton->setStyleSheet(styleT);

    //Background
    QBrush colB = format.background();
    QString styleB = "QPushButton {color:";
    styleB.append(colB.color().name());
    styleB.append("}");
    ui->backgroundTextColor->setStyleSheet(styleB);
}



void NewMessage::on_messageText_cursorPositionChanged()
{
    //Align
    Qt::Alignment align = ui->messageText->alignment();
    switch(align)
    {
    case 0x0001 | 0x0010:
        {
            ui->leftAlignButton->setChecked(true);
            break;
        }
    case Qt::AlignHCenter:
        {
            ui->centerAlignButton->setChecked(true);
            break;
        }
    case 0x0002 | 0x0010:
        {
            ui->rightAlignButton->setChecked(true);
            break;
        }
    case Qt::AlignJustify:
        {
            ui->justifyAlignButton->setChecked(true);
            break;
        }
    case 0x0001:
        {
            ui->leftAlignButton->setChecked(true);
            break;
        }
    default:
        {
            break;
        }
    }
}

void NewMessage::AutoSuggest()
{
    QLineEdit* edit;
    edit = (QLineEdit *) sender();
    suggest->ShowSuggest(edit,edit->text());
}

void NewMessage::on_fromComboBox_currentIndexChanged(const QString &arg1)
{
    fromMail = arg1;
}
